package com.itboye.lottery.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.lottery.R;
import com.itboye.lottery.bean.IndexDataBean;
import com.itboye.lottery.fragment.HomeFragment;
import com.itboye.lottery.utils.Const;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wu on 2018/1/9.
 */

public class HomeBottomFastTouZhuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private HomeFragment fragment;
    private Context context;
    List<IndexDataBean.Fast_arr> classEntityArrayList = new ArrayList<>();

    public HomeBottomFastTouZhuAdapter(HomeFragment fragment, List<IndexDataBean.Fast_arr> classEntityArrayList) {
        this.classEntityArrayList = classEntityArrayList;
        this.fragment = fragment;
    }

    public OnItemClickListener itemClickListener;

    public void setOnItemClickListener(HomeBottomFastTouZhuAdapter.OnItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }


    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolderChild(LayoutInflater.from(context).inflate(R.layout.item_home_bottom_classifycation_type_1, parent, false));
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ViewHolderChild viewHolder = (ViewHolderChild) holder;
        viewHolder.ll_number.removeAllViews();
        IndexDataBean.Fast_arr fastArr = classEntityArrayList.get(position);

        viewHolder.tv_home_bottom_classifycation_style_1_title.setText(fastArr.getName());
        viewHolder.tv_home_bottom_classifycation_style_1_jiangchi.setText("奖池" + fastArr.getInfo().getNext_all_money() + "元");
        viewHolder.tv_home_bottom_classifycation_style_1_qishu.setText("第" + fastArr.getInfo().getNumber() + "期 " + fastArr.getOpentime() + "开奖");
        viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setOnClickListener(fragment);//立即投注
        viewHolder.tv_home_bottom_classifycation_style_1_refresh.setOnClickListener(fragment);//刷新号码
        viewHolder.tv_home_bottom_classifycation_style_1_refresh.setTag(position);
        viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setTag(position);
        viewHolder.tv_home_bottom_classifycation_style_1_load_more.setTag(position);
        viewHolder.tv_home_bottom_classifycation_style_1_load_more.setOnClickListener(fragment);//更多
        viewHolder.tv_home_bottom_classifycation_style_1_stop_sale.setVisibility(View.GONE);
        viewHolder.tv_home_bottom_classifycation_style_1_load_more.setVisibility(View.VISIBLE);
        viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setBackgroundResource(R.drawable.rounded_bg_red_border_none);
        viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setEnabled(true);

        viewHolder.tv_home_bottom_classifycation_style_1_title.setTextColor(context.getResources().getColor(R.color.colorAccent));
        viewHolder.tv_home_bottom_classifycation_style_1_jiangchi.setTextColor(context.getResources().getColor(R.color.colorAccent));

        //处理暂停销售
        boolean isStop=false;
        if ((fastArr.getId() == 1001 && !Const.Companion.getShuangseqiu())||(fastArr.getId() == 2001 && !Const.Companion.getDaletou())) {
            //双色球
            viewHolder.tv_home_bottom_classifycation_style_1_stop_sale.setVisibility(View.VISIBLE);
            viewHolder.tv_home_bottom_classifycation_style_1_load_more.setVisibility(View.GONE);
            viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setBackgroundResource(R.drawable.rounded_bg_gray_border_none);
            viewHolder.tv_home_bottom_classifycation_style_1_touzhu.setEnabled(false);
            viewHolder.tv_home_bottom_classifycation_style_1_title.setTextColor(context.getResources().getColor(R.color.gray_9f9f9f));
            viewHolder.tv_home_bottom_classifycation_style_1_jiangchi.setTextColor(context.getResources().getColor(R.color.gray_9f9f9f));
            isStop=true;
        }

        for (int i = 0; i < fastArr.getArrayListCode().size(); i++) {
            TextView textView = new TextView(context);
            if (isStop) {
                textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hollow_circle_border_gray));
                textView.setTextColor(context.getResources().getColor(R.color.gray_9f9f9f));
            }else {
                if (fastArr.getArrayListCode().get(i).getType() == 0) {
                    //红球
                    textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hollow_circle_border_red));
                    textView.setTextColor(context.getResources().getColor(R.color.main_color));
                } else if (fastArr.getArrayListCode().get(i).getType() == 2) {
                    //蓝球
                    textView.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.hollow_circle_border_blue));
                    textView.setTextColor(context.getResources().getColor(R.color.blue_75b4e2));
                }
            }
            textView.setText(fastArr.getArrayListCode().get(i).getCodeText());
            textView.setGravity(Gravity.CENTER);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.setMargins(10, 10, 0, 10);
            viewHolder.ll_number.addView(textView, layoutParams);
        }
    }

    @Override
    public int getItemCount() {
        return classEntityArrayList == null ? 0 : classEntityArrayList.size();
    }

    public class ViewHolderChild extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img,tv_home_bottom_classifycation_style_1_stop_sale;
        public TextView name, tv_home_bottom_classifycation_style_1_title, tv_home_bottom_classifycation_style_1_jiangchi, tv_home_bottom_classifycation_style_1_qishu, tv_home_bottom_classifycation_style_1_touzhu, tv_home_bottom_classifycation_style_1_load_more, tv_home_bottom_classifycation_style_1_refresh;
        public LinearLayout ll_number;

        public ViewHolderChild(View itemView) {
            super(itemView);
            ll_number = itemView.findViewById(R.id.ll_number);
            tv_home_bottom_classifycation_style_1_title = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_title);
            tv_home_bottom_classifycation_style_1_jiangchi = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_jiangchi);
            tv_home_bottom_classifycation_style_1_qishu = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_qishu);
            tv_home_bottom_classifycation_style_1_touzhu = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_touzhu);
            tv_home_bottom_classifycation_style_1_load_more = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_load_more);
            tv_home_bottom_classifycation_style_1_refresh = itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_refresh);
            tv_home_bottom_classifycation_style_1_stop_sale=itemView.findViewById(R.id.tv_home_bottom_classifycation_style_1_stop_sale);
//            img = itemView.findViewById(R.id.img);
//            name = itemView.findViewById(R.id.name);
//
//            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            if (itemClickListener != null) {
//                itemClickListener.onItemClick(v, getAdapterPosition());
//            }
        }
    }

    public class ViewHolderChild2 extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img;
        public TextView name;

        public ViewHolderChild2(View itemView) {
            super(itemView);
//            img = itemView.findViewById(R.id.img);
//            name = itemView.findViewById(R.id.name);
//
//            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            if (itemClickListener != null) {
//                itemClickListener.onItemClick(v, getAdapterPosition());
//            }
        }
    }

    public class ViewHolderTitle extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ImageView img;
        public TextView name;

        public ViewHolderTitle(View itemView) {
            super(itemView);
//            img = itemView.findViewById(R.id.img);
//            name = itemView.findViewById(R.id.name);
//
//            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
//            if (itemClickListener != null) {
//                itemClickListener.onItemClick(v, getAdapterPosition());
//            }
        }
    }
}
